/* Copyright 2014 Sheldon Neilson www.neilson.co.za
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
package com.moco.launcher.alarm;

import java.lang.reflect.Field;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewConfiguration;

import com.moco.launcher.activity.ShutDownActivity;
import com.moco.launcher.link.LinkManager;
import com.oazon.system.bean.SystemState;

public abstract class BaseActivity extends Activity implements
		android.view.View.OnClickListener {

	public static final String TAG = "BaseActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			ViewConfiguration config = ViewConfiguration.get(this);
			Field menuKeyField = ViewConfiguration.class
					.getDeclaredField("sHasPermanentMenuKey");
			if (menuKeyField != null) {
				menuKeyField.setAccessible(true);
				menuKeyField.setBoolean(config, false);
			}
		} catch (Exception ex) {
			// Ignore
		}
	}

	private int keyDownCount = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d(TAG, "----> 按键按下:" + keyCode + " event:" + event.getAction());
		if (keyCode == 26) {
			keyDownCount++;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		Log.d(TAG, "----> 按键释放:" + keyCode + " event:" + event.getAction());

		if (keyCode == 26) {
			if (keyDownCount == 1) {
				keyDownCount = 0;
				// TODO 短按
				if (LinkManager.getInstance().getSaveSystemState() == SystemState.NORMAL) {
					LinkManager.getInstance().setScreenProtect();
				} else {
					LinkManager.getInstance().setNormal();
				}
				Log.d(TAG, "短按");
			} else {
				keyDownCount = 0;
				// TODO 长按
				Intent intent = new Intent(this, ShutDownActivity.class);
				startActivity(intent);
				Log.d(TAG, "长按");
			}
		}

		return super.onKeyUp(keyCode, event);
	}

	protected void callMathAlarmScheduleService() {
		Intent mathAlarmServiceIntent = new Intent(this,
				AlarmServiceBroadcastReciever.class);
		sendBroadcast(mathAlarmServiceIntent, null);
	}
}
